﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace aPCSuite
{
    public partial class frmReply : Form
    {
        private SMSFramework _SMSFramework = new SMSFramework();
        public event RefereshSMS _refresh_sms = null;
        public frmReply()
        {
            InitializeComponent();
            _SMSFramework.parent = this;
            ContextMenu cm = new ContextMenu();
            MenuItem mi = new MenuItem("Paste");
            mi.Click += new EventHandler(PasteAction);
            cm.MenuItems.Add(mi);
            richTextBox1.ContextMenu = cm;
        }

        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                richTextBox1.Text
                    += Clipboard.GetText(TextDataFormat.Text).ToString();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string req = "<protocol encoding=\"UTF-8\" version=\"1.0\">";
                req += "<app_id>1</app_id>";
                req += "<session_id>" + Transaction._session_id + "</session_id>";
                req += "<sms_send>";
                req += "<dev_code>" + aPCSuite.Properties.Settings.Default.dev_code + "</dev_code>";
                req += "<address>" + txtTo.Text + "</address>";
                req += "<content>" + richTextBox1.Text + "</content>";
                req += "</sms_send>";
                req += "</protocol>";
                _SMSFramework.srequest = req;
                _SMSFramework._callback += new callback(_callback);
                _SMSFramework.Start();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("btnOK_Click exception : {0}", ex.Message);
            }
        }

        public void SetReplyAddress(string address)
        {
            txtTo.Text = address;
            this.Refresh();
        }

        private void _callback(object sender, EventArgs e)
        {
            try
            {
                protocol ret = sender as protocol;
                if (ret != null && ret.code == Transaction.OK)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        this.Close();
                        if (_refresh_sms != null)
                            _refresh_sms(sender, e);
                    }));
                }
                else
                {
                    //this.Invoke(new MethodInvoker(delegate { this.Close(); }));
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("frmContact _callback : {0}", ex.Message);
            }/**/
        }
    }
}
